<?php
	/**
	 * author: hutao
	 * date: 2011-4-22
	 */

	class Sql
	{
		function insert($arr , $table, $updatefield = array(), $changefield = array())
		{
			$sql = "insert into $table (";
			$flag = 0;
			foreach($arr as $k => $v)
			{
				$sql .= ($flag==0?"":", ").$k;
				$flag = 1;
			}
			$sql .= ") values(";
			$flag = 0;
			foreach($arr as $k => $v)
			{
				$sql .= ($flag==0?"'":", '").mysql_escape_string($v)."'";
				$flag = 1;
			}
			$sql .= ")";
			if (!empty($updatefield) || !empty($changefield))
			{
				$sql .= " ON DUPLICATE KEY UPDATE ";
				$flag = 0;
				foreach ($updatefield as $v)
				{
					$sql .= ($flag==0?"":", ").$v." = '".mysql_escape_string($arr[$v])."'";
					$flag = 1;
				}
				foreach ($changefield as $k => $v)
				{
					if (is_numeric($v))
					{
						$sql .= ($flag==0?"":", ").$k."=".$k.($v >= 0 ? "+" : "").($v);
					}
					else
					{
						$sql .= ($flag==0?"":", ").$k."=CONCAT(".$k.",'".mysql_escape_string($v)."')";
					}
					$flag = 1;
				}
			}
	
			return $sql;
		}
		
		//向同一个表批量插入大量数据的时候，用这个函数效率高
		function insertMore($arr, $table)
		{
			$tmpArr = $arr[0];
	
			$sql = "insert into $table (";
			$flag = 0;
			foreach($tmpArr as $k => $v)
			{
				$sql .= ($flag==0?"":", ").$k;
				$flag = 1;
			}
			$sql .= ") values";
			foreach ($arr as $varr)
			{
				$flag = 0;
				$sql.="(";
				foreach($varr as $k => $v)
				{
					$sql .= ($flag==0?"'":", '").mysql_escape_string($v)."'";
					$flag = 1;
				}
				$sql .= "),";
			}
			$sql = trim($sql,' ,');
	
			return $sql;
		}
	
		function replace($arr , $table)
		{
			$sql = "replace into $table (";
			$flag = 0;
			foreach($arr as $k => $v)
			{
				$sql .= ($flag==0?"":", ").$k;
				$flag = 1;
			}
			$sql .= ") values(";
			$flag = 0;
			foreach($arr as $k => $v)
			{
				$sql .= ($flag==0?"'":", '").mysql_escape_string($v)."'";
				$flag = 1;
			}
			$sql .= ")";
	
			return $sql;
		}
		
		//向同一个表批量插入大量数据的时候，用这个函数效率高
		function replaceMore($arr, $table)
		{
			$tmpArr = $arr[0];
	
			$sql = "replace into $table (";
			$flag = 0;
			foreach($tmpArr as $k => $v)
			{
				$sql .= ($flag==0?"":", ").$k;
				$flag = 1;
			}
			$sql .= ") values";
			foreach ($arr as $varr)
			{
				$flag = 0;
				$sql.="(";
				foreach($varr as $k => $v)
				{
					$sql .= ($flag==0?"'":", '").mysql_escape_string($v)."'";
					$flag = 1;
				}
				$sql .= "),";
			}
			$sql = trim($sql,' ,');
	
			return $sql;
		}
	
		function update($arr , $table , $where)
		{
			$sql = "update $table set ";
			$flag = 0;
			foreach($arr as $k => $v)
			{
				$sql .= ($flag==0?"":", ").$k." = '".mysql_escape_string($v)."'";
				$flag = 1;
			}
			$sql .= " ".$where;
	
			return $sql;
		}
	
		function updateEx($arr , $change, $table , $where)
		{
			$sql = "update $table set ";
			$flag = 0;
			foreach($arr as $k => $v)
			{
				$sql .= ($flag==0?"":", ").$k." = '".mysql_escape_string($v)."'";
				$flag = 1;
			}
			foreach($change as $k => $v)
			{
				if (is_numeric($v))
				{
					$sql .= sprintf("%s %s=%s%s%s", ($flag==0 ? "" : ","),
						$k, $k, ($v >= 0 ? "+" : " "), $v);
					$flag = 1;
				}
				else 
				{
					$sql .= sprintf("%s %s=CONCAT(%s,'%s')", ($flag==0 ? "" : ","),
						$k, $k, mysql_escape_string($v));
					$flag = 1;
				}
			}
			$sql .= " ".$where;
	
			return $sql;
		}
	}
?>
